//
//  CMSegmentControl.m
//  CraneMachine
//
//  Created by ice on 2017/10/31.
//  Copyright © 2017年 ICE. All rights reserved.
//

#import "CMSegmentControl.h"
#import "UIView+Frame.h"

@interface CMSegmentControl ()

@property (nonatomic, strong) UIImageView *mobileBar;
@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) NSInteger sections;
@property (nonatomic, copy) NSArray *items;
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, strong) NSMutableArray *countLabels;

@property (nonatomic, strong) UIColor *mobileCorlor;
@property (nonatomic, strong) UIColor *selectedColor;

@end

@implementation CMSegmentControl

- (id)initWithFrame:(CGRect)frame
              Items:(NSArray *)items {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.sections = items.count;
        self.width = CGRectGetWidth(self.frame);
        self.height = CGRectGetHeight(self.frame);
        self.items = items;
        
        [self setupButtons];
        [self setupMoblieBar];
    }
    return self;
}

- (void)setupButtons
{
    CGFloat itemWidth = self.width/self.sections;
    
    !self.buttons ?
    self.buttons = [[NSMutableArray alloc] initWithCapacity:_items.count]:
    [self.buttons removeAllObjects];
    
    _countLabels = [NSMutableArray new];
    
    //根据传入的数组生成对应的按键
    for (int i = 0; i < _items.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i *itemWidth, 0, itemWidth, self.height);
        btn.tag = i;
        [btn setTitle:_items[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        if (i == 0) {//默认第一个选中
            [btn setSelected:YES];
        }
        if (btn.imageView.frame.size.width != 0) {
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width, 0, btn.imageView.frame.size.width);
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width, 0, -btn.titleLabel.bounds.size.width);
        }
        [self.buttons addObject:btn];
    }
}

- (void)setupMoblieBar
{
    self.mobileBar = [UIImageView new];
    self.mobileBar.frame = CGRectMake(0, self.height - 2, self.width/self.sections, 2);
    self.bottomLine = [UIView new];
    self.bottomLine.frame = CGRectMake(0, self.height - 1, SCREEN_WIDTH, 1);
//    self.bottomLine.backgroundColor = [UIColor grayColor];
    [self addSubview:self.bottomLine];
    [self addSubview:self.mobileBar];
}

- (void)setMobileBarColor:(UIColor *)color
{
    _mobileCorlor = color;
    self.mobileBar.backgroundColor = _mobileCorlor ? : [UIColor colorWithRed:0.384 green:0.769 blue:0.969 alpha:1.000];
}

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
{
    if (state == UIControlStateSelected) {
        _selectedColor = color;
    }
    for (UIButton *btn in self.buttons) {
        [btn setTitleColor:color forState:state];
    }
}
- (void)buttonOnClick:(UIButton *)button {
    
    //处理移动条动画
    [UIView animateWithDuration:0.25 animations:^{
        CGFloat distance = self.width/self.sections;
        CGFloat x = (self.width / self.sections - _mobileBar.width) / 2;
        self.mobileBar.frame = CGRectMake(x + distance * button.tag, self.height - 2, _mobileBar.width, _mobileBar.height);
    }];
    [self setButtonSelectedStatus:button];
}

//清空所有选择状态,设置某一选中
- (void)setButtonSelectedStatus:(UIButton *)button {
    for (UIButton *btn in self.buttons) {
        if (btn.tag != button.tag) {
            btn.selected = NO;
        }
    }
    button.selected = YES;
}

@end
